package com.shen.aeditor

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

import com.shen.aeditor.databinding.ActivityMainBinding
import com.yesemate.splayer.PLAY_MODEL_DEFAULT
import com.yesemate.splayer.PLAY_MODEL_FRAME_PREVIEW
import com.yesemate.splayer.Splayer

import com.yesemate.splayer.helper.IFFmpegHelper
import com.yesemate.splayer.util.TimeInfoBean
import com.yesemate.splayer.util.logD
import java.io.File

class MainActivity : AppCompatActivity() {

    private  val TAG = "MainActivity"
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var file=File("/storage/emulated/0/avier1.mp4")
        if (file.exists()){
            Log.e(TAG,"文件存在")
        }else{
            Log.e(TAG,"n文件存在")
        }

        binding.start.setOnClickListener {

        }
        XXPermissions.with(this)
            // 申请单个权限
            .permission(Permission.CAMERA)

            .permission(Permission.MANAGE_EXTERNAL_STORAGE)
            .permission(Permission.RECORD_AUDIO)
//            .permission(Permission.READ_MEDIA_IMAGES)
//            .permission(Permission.READ_MEDIA_VIDEO)
//            .permission(Permission.READ_MEDIA_AUDIO)
            // 申请多个权限

            // 设置权限请求拦截器（局部设置）
            //.interceptor(new PermissionInterceptor())
            // 设置不触发错误检测机制（局部设置）
            //.unchecked()
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
//                        toast("获取部分权限成功，但部分权限未正常授予")
                        return
                    }
                    Log.e(TAG,"setPreview")
                    setPreview();
//                    toast("获取录音和日历权限成功")
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    if (doNotAskAgain) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(applicationContext, permissions)
                    } else {
//                        toast("获取录音和日历权限失败")
                    }
                }
            })



    }

    var splayer:Splayer?=null


    private fun setPreview(){

        var inputPath="/storage/emulated/0/avier1.mp4"
        splayer=Splayer()
        splayer?.source=inputPath
        splayer?.setPlayModel1(PLAY_MODEL_FRAME_PREVIEW)
        splayer?.prepare()

        splayer?.mPlayerListener=object :Splayer.PlayerListener{
            override fun onError(code: Int, msg: String?) {
                Log.e(TAG,"onError")
            }

            override fun onPrepare() {
                Log.e(TAG,"onPrepare")
               splayer?.start()

            }

            override fun onLoadChange(isLoad: Boolean) {
                Log.e(TAG,"onLoadChange")
            }

            override fun onProgress(currentTime: Long, totalTime: Long) {
                Log.e(TAG,"onProgress")
            }

            override fun onTimeInfo(timeInfoBean: TimeInfoBean?) {
                Log.e(TAG,"onTimeInfo")
            }

            override fun onEnablePlayChange(enable: Boolean) {
                Log.e(TAG,"onEnablePlayChange")
            }

            override fun onPlayStop() {
                Log.e(TAG,"onPlayStop")
            }

            override fun onComplete() {
                Log.e(TAG,"onComplete")
            }

            override fun onDB(db: Int) {
                Log.e(TAG,"onDB")
            }

            override fun onGetVideoInfo(fps: Int, duration: Long, widht: Int, height: Int) {
                Log.e(TAG,"onGetVideoInfo")
            }

        }

        val avFrameHelper = IFFmpegHelper(inputPath,null)
        avFrameHelper.setFFPlayer(splayer!!)
    }

}